package com.douban.crawl.crawler;

import com.douban.crawl.db.MovieDao;
import com.douban.crawl.model.Movie;
import com.douban.crawl.utils.CommonUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 电影抓取
 */
public class MovieCrawler {
    public static void main(String[] args) {

        CommonUtil.setGlobalIPAndPort();

        String url = "https://movie.douban.com/top250";
        String pageInfo = "";
        String tempUrl = "";
        for (int i = 1; i <= 10; i++) {
            try {
                if (i != 1) {
                    pageInfo = "?start=" + 25 * (i -1) +"&filter=";
                    tempUrl = url + pageInfo;
                    getMovieHtml(tempUrl);
                } else {
                    tempUrl = url;
                    getMovieHtml(tempUrl);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void getMovieHtml(String url) throws IOException, InterruptedException {
        MovieDao movieDao = new MovieDao();
        String header = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36";
        Document doc = Jsoup.connect(url).header("User-Agent", header).get();
        Elements movieList = doc.body().getElementsByClass("item");
        for (Element movieEle: movieList) {
            Element picEle = movieEle.getElementsByClass("pic").get(0);
            String movieHref = picEle.getElementsByTag("a").attr("href");

            // 避免频繁请求被封锁IP
            TimeUnit.MILLISECONDS.sleep(1000);

            // 获取豆瓣电影id
            String movieNumber = CommonUtil.extractNumber(movieHref);

            Document movieDetailDoc = Jsoup.connect(movieHref).get();
            Element movieContent = movieDetailDoc.getElementById("content");
            // 获取电影海报图片链接
            String moviePosterUrl = movieContent.getElementById("mainpic").getElementsByTag("img").get(0).attr("src");
            // 获取电影名
            String movieName = movieContent.getElementsByTag("h1").get(0).getElementsByTag("span").get(0).text();

            Elements personList = movieContent.getElementsByClass("attrs");
            // 获取导演信息
            String directors = "";
            if (personList != null && personList.size() >= 1) {
                Elements directorList = personList.get(0).getElementsByTag("a");
                directors = appendListToStr(directorList);
            }

            // 获取编剧信息
            String screenwriters = "";
            if (personList != null && personList.size() >= 2) {
                Elements screenwriterList = personList.get(1).getElementsByTag("a");
                screenwriters = appendListToStr(screenwriterList);
            }

            // 获取演员信息
            String performers = "";
            if (personList != null && personList.size() >= 3) {
                Elements performerList = personList.get(2).getElementsByTag("a");
                performers = appendListToStr(performerList);
            }

            // 获取电影类型
            Elements movieTypeList = movieContent.getElementsByAttributeValue("property", "v:genre");
            String movieTypes = appendListToStr(movieTypeList);

            // 获取制片国家/地区
            String region = movieContent.getElementsByClass("pl").get(4).nextSibling().toString();

            // 语言
            String language = movieContent.getElementsByClass("pl").get(5).nextSibling().toString();

            // 上映日期
            Elements releaseDateList = movieContent.getElementsByAttributeValue("property", "v:initialReleaseDate");
            String releaseDates = appendListToStr(releaseDateList);

            // 片长
            Elements filmLengthList = movieContent.getElementsByAttributeValue("property", "v:runtime");
            String filmLengths = appendListToStr(filmLengthList);

            // 别名
            String aliasName = movieContent.getElementsByClass("pl").get(8).nextSibling().toString();

            // IMDb
            String imdb = movieContent.getElementsByClass("pl").get(9).nextSibling().toString();

            // 评分
            Elements scoreList = movieContent.getElementsByAttributeValue("property", "v:average");
            String score = appendListToStr(scoreList);

            // 简介
            Elements briefIntroductionList = movieDetailDoc.getElementsByAttributeValue("property", "v:summary");
            String briefIntroduction = appendListToStr(briefIntroductionList);

            StringBuilder res = new StringBuilder();
            res.append("豆瓣电影id:").append(movieNumber).append(System.lineSeparator())
                    .append("电影海报图片链接:").append(moviePosterUrl).append(System.lineSeparator())
                    .append("电影名:").append(movieName).append(System.lineSeparator())
                    .append("导演信息:").append(directors).append(System.lineSeparator())
                    .append("编剧信息:").append(screenwriters).append(System.lineSeparator())
                    .append("演员信息:").append(performers).append(System.lineSeparator())
                    .append("电影类型:").append(movieTypes).append(System.lineSeparator())
                    .append("制片国家/地区:").append(region).append(System.lineSeparator())
                    .append("语言:").append(language).append(System.lineSeparator())
                    .append("上映日期:").append(releaseDates).append(System.lineSeparator())
                    .append("片长:").append(filmLengths).append(System.lineSeparator())
                    .append("别名:").append(aliasName).append(System.lineSeparator())
                    .append("IMDb:").append(imdb).append(System.lineSeparator())
                    .append("评分:").append(score).append(System.lineSeparator())
                    .append("简介:").append(briefIntroduction).append(System.lineSeparator());
            System.out.println(res);

            Movie movie = new Movie(movieNumber, moviePosterUrl,
                    movieName, directors, screenwriters, performers,
                    movieTypes, region, language, releaseDates, filmLengths,
                    aliasName, imdb, briefIntroduction, score);

            movieDao.save(movie);
        }
    }

    /**
     * 将列表拼接成字符串
     * @param elements
     * @return
     */
    public static String appendListToStr(Elements elements) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < elements.size(); i++) {
            if (i == elements.size() - 1) {
                str.append(elements.get(i).text());
            } else {
                str.append(elements.get(i).text()).append("/");
            }
        }

        return str.toString();
    }
}
